﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ShopBLL;
using ShopModel;

public partial class controls_ctrlProductChoose : System.Web.UI.UserControl
{
    #region CONSTANTS

    private const string CATEGORY_LIST = "CategoryListItems";
    private const string PRODUCT_LIST = "ProductListItems";

    #endregion CONSTANTS

    #region VARIABLES

    private bool availableProductsOnly;
    private bool newProduct;
    private bool showBundle;

    private bool _BindControl = true;
    private bool _AutoPostBack;
    
    private int _IDFilter = -1;
    private string _NameFilter = string.Empty;

    private static List<Products> productsList = null;
    private static List<DictionaryValue> categoriesList = null;
    private static bool RefreshedLists = false;

    public event EventHandler ProductListIndexChanged;

    private bool dispalyLabels = true;

    #endregion VARIABLES

    #region PROPERTIES

    public int ProductId
    {
        get
        {
            int id = -1;
            int.TryParse(ddlProducts.SelectedValue, out id);

            return id;
        }

        set
        {
            if (ddlProducts.Items.FindByValue(value.ToString()) != null)
            {
                ddlProducts.SelectedValue = value.ToString();
            }
        }
    }

    public string ProductName
    {
        get { return ddlProducts.SelectedItem.Text; }
    }

    public int CategoryId
    {
        get
        {
            int id = -1;
            int.TryParse(hfSelectedCategoryID.Value, out id);

            return id;
        }

        set
        {
            if (ddlCategory.Items.FindByValue(value.ToString()) != null)
            {
                ddlCategory.SelectedValue = value.ToString();
                ddlProducts.DataSource = ProductsList;
                ddlProducts.DataBind();
            }
        }
    }

    public bool AutoPostBack
    {
        get
        {
            return _AutoPostBack;
        }

        set
        {
            _AutoPostBack = value;
        }
    }

    public bool BindControl
    {
        get { return _BindControl; }
        set { _BindControl = value; }
    }

    public bool ShowBundle
    {
        get { return showBundle; }
        set { showBundle = value; }
    }

    public bool Enalbed
    {
        set
        {
            ddlProducts.Enabled = value;
            ddlCategory.Enabled = value;
        }
    }

    public bool AvailableProductsOnly
    {
        get { return availableProductsOnly; }
        set { availableProductsOnly = value; }
    }

    public bool NewProduct
    {
        get { return newProduct; }
        set
        {
            newProduct = value;
        }
    }

    public int IDFilter
    {
        get { return _IDFilter; }
        set { _IDFilter = value; }
    }

    public string NameFilter
    {
        get { return _NameFilter; }
        set { _NameFilter = value; }
    }

    private List<Products> ProductsList
    {
        get
        {
            if (productsList == null)
            {
                if (!IsPostBack || Session[PRODUCT_LIST] == null)
                {
                    productsList = ProductsManager.Instance.GetProducts(null, null, null, null, null, false, CommonTools.CurrentLanguageID);
                    productsList.Sort(delegate(Products p1, Products p2) { return p1.Name.CompareTo(p2.Name); });
                    Session[PRODUCT_LIST] = productsList;
                }
                else
                    productsList = (List<Products>)Session[PRODUCT_LIST];
            }

            List<int> parentList = new List<int>();

            if (CategoryId != -1)
                parentList = ProductsManager.Instance.GetSubcategoriesIDs(CategoryId);

            return productsList.FindAll(delegate(Products p)
            {
                if (CategoryId != -1 && !parentList.Contains(p.CategoryID))
                    return false;
                if (availableProductsOnly && p.AvailableCount == 0)
                    return false;
                if (!ShowBundle && p.IsBundle)
                    return false;
                if (NameFilter != string.Empty && !p.Name.ToLower().Contains(NameFilter.ToLower()))
                    return false;
                if (IDFilter > 0 && p.IDproduct != IDFilter)
                    return false;

                return true;
            });
        }
    }

    private List<DictionaryValue> CategoriesList
    {
        get
        {
            if (categoriesList == null)
            {
                if (Session[CATEGORY_LIST] == null)
                {
                    Category categoryTree = ProductsManager.Instance.GetChildernTreeCategories(-1, true, ShopBLL.CommonTools.CurrentLanguageID);

                    categoriesList = new List<DictionaryValue>();
                    ShopBLL.CommonTools.CreateCategoryTree(categoryTree, categoriesList, "", true);

                    categoriesList.Sort(delegate(DictionaryValue d1, DictionaryValue d2)
                    {
                        return d1.Name.CompareTo(d2.Name);
                    });

                    Session[CATEGORY_LIST] = categoriesList;
                }
                else
                    categoriesList = (List<DictionaryValue>)Session[CATEGORY_LIST];
            }

            return categoriesList;
        }
    }

    public bool FilterMatch
    {
        get 
        {
            if (IDFilter > 0 || NameFilter != string.Empty)
                return ddlProducts.Items.Count > 0 + (NewProduct ? 1 : 0);
            else 
                return true;
        }
    }

    public bool DisplayLabels
    {
        get { return dispalyLabels; }
        set { dispalyLabels = value; }
    }

    #endregion PROPERTIES

    #region METHODS

    public bool SelectProduct(int ProductID)
    {
        string value = ProductID.ToString();
        if (ddlProducts.Items.FindByValue(value) != null)
        {
            ddlProducts.SelectedValue = value;
            return true;
        }
        else if (ddlCategory.SelectedValue != "-1")
        {
            ddlCategory.SelectedValue = "-1";
            ddlProducts.DataSource = ProductsList;
            ddlProducts.DataBind();

            if (ddlProducts.Items.FindByValue(value) != null)
            {
                ddlProducts.SelectedValue = value;
                return true;
            }
        }

        return false;
    }

    public void SetControlToNewProduct()
    {
        if (newProduct)
        {
            ddlCategory.SelectedValue = "-1";
            ddlProducts.SelectedValue = "-1";
        }
    }

    public void RemoveProduct(int ProductID)
    {
        ListItem item = ddlProducts.Items.FindByValue(ProductID.ToString());
        if (item != null)
            item.Enabled = false;
    }

    public void SetProductsList(List<Products> ProductList)
    {
        ddlProducts.Items.Clear();
        ddlProducts.DataSource = ProductList;
        ddlProducts.DataBind();
    }

    public void RebindAndSelectProducts(int ProductID, int CategoryID)
    {
        productsList = null;
        Session[PRODUCT_LIST] = null;

        if (CategoryID != -1)
        {
            hfSelectedCategoryID.Value = CategoryID.ToString();
            ddlCategory.SelectedValue = CategoryID.ToString();
        }

        ddlProducts.Items.Clear();

        if (newProduct)
            ddlProducts.Items.Add(new ListItem(Resources.Language.NewProduct, "-1"));

        ddlProducts.DataSource = ProductsList;
        ddlProducts.DataBind();

        if (ddlProducts.Items.FindByValue(ProductID.ToString()) != null)
        {
            hfSelectedProductID.Value = ProductID.ToString();
            ddlProducts.SelectedValue = ProductID.ToString();
        }
        else
        {
            ddlProducts.SelectedIndex = 0;
            hfSelectedProductID.Value = ddlProducts.SelectedValue;
        }
    }

    public void UpdateProductName(int ProductID, string Name)
    {
        if (ProductID > 0)
        {
            productsList.Find(delegate(Products p) { return p.IDproduct == ProductID; }).Name = Name;
            Session[PRODUCT_LIST] = productsList;
        }

        ddlProducts.Items.FindByValue(ProductID.ToString()).Text = Name;
    }

    public void Bind()
    {
        ddlCategory.Items.Add(new ListItem(Resources.Language.AllCategories, "-1"));

        ddlCategory.DataSource = CategoriesList;
        ddlCategory.DataBind();

        if (newProduct)
            ddlProducts.Items.Add(new ListItem(Resources.Language.NewProduct, "-1"));

        ddlProducts.DataSource = ProductsList;
        ddlProducts.DataBind();
    }

    #endregion METHODS

    #region PAGE EVENTS

    protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack && !RefreshedLists)
        {
            categoriesList = null;
            Session[CATEGORY_LIST] = null;
            productsList = null;
            Session[PRODUCT_LIST] = null;
            RefreshedLists = true;
        }

        if (BindControl)
            Bind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (ddlCategory.SelectedValue != "-1" || IDFilter > 0 || NameFilter != string.Empty)
        {
            string NewSelectedValue = ddlProducts.SelectedValue;

            hfSelectedCategoryID.Value = ddlCategory.SelectedValue;

            ddlProducts.Items.Clear();

            if (newProduct)
                ddlProducts.Items.Add(new ListItem(Resources.Language.NewProduct, "-1"));

            ddlProducts.DataSource = ProductsList;
            ddlProducts.DataBind();

            ProductId = int.Parse(NewSelectedValue);
        }

        if (hfSelectedProductID.Value != ddlProducts.SelectedValue)
        {
            hfSelectedProductID.Value = ddlProducts.SelectedValue;

            if (ProductListIndexChanged != null)
                ProductListIndexChanged(this, e);
        }

        RefreshedLists = false;

        label1.Visible = dispalyLabels;
        label2.Visible = dispalyLabels;
    }

    #endregion PAGE EVENTS

}

